package com.example.vpdemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class MaterialMainActivity extends AppCompatActivity implements View.OnClickListener ,Icho {

    private ViewPager vp;
    private TabLayout tab;
    ArrayList<Fragment> fragments;
    ArrayList<String> titles;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private FrameLayout fl;
    FragmentA fragmentA;
    FragmentA fragmentA2;
    FragmentB fragmentB;
    private TextView tv_img;
    private ImageView iv_img;
    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_materialmain);
        tv_img = (TextView) findViewById(R.id.tv_img);
        iv_img = (ImageView) findViewById(R.id.iv_image);
        tv_img.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        vp = (ViewPager) findViewById(R.id.vp);
        tab = (TabLayout) findViewById(R.id.tab);

        fl = (FrameLayout) findViewById(R.id.fl);
        fragmentA = new FragmentA();
        fragmentA2 = new FragmentA();
        fragmentB = new FragmentB();
        fragments = new ArrayList<>();
        fragments.add(fragmentA);
        fragments.add(fragmentA2);
        fragments.add(fragmentB);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        titles = new ArrayList<>();
        titles.add("A1");
        titles.add("A2");
        titles.add("B");


        FPAdater fpAdater = new FPAdater(getSupportFragmentManager(), fragments, titles);

        vp.setAdapter(fpAdater);
        tab.setupWithViewPager(vp);//与viewpager联系起来

//        addFragment(fragmentA);

        iv_img.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {

                                          Toast.makeText(MaterialMainActivity.this, "dianji", Toast.LENGTH_SHORT).show();
                                          FragmentTransaction transaction = null;
                                          for (int i = 0; i < fragments.size(); i++) {
                                              if (fragments.get(i) instanceof FragmentA) {
                                                  transaction = getSupportFragmentManager().beginTransaction();
                                                  if (Build.VERSION.SDK_INT >= 26) {
                                                      transaction.setReorderingAllowed(false);
                                                  }
                                                  transaction.detach(fragments.get(i));
                                                  transaction.attach(fragments.get(i));
                                                  transaction.commit();
                                              }
                                          }


           }

        }
        );



    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btn1:
//                addFragment(fragmentA);
//                break;
//            case R.id.btn2:
//                addFragment(fragmentB);
//                break;
//            case R.id.btn3:
//                break;
        }

    }

    //向Activity中添加Fragment的方法
    public void addFragment(Fragment fragment) {


        //获得Fragment管理器
        FragmentManager fragmentManager = getSupportFragmentManager();
        //使用管理器开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //使用事务替换Fragment容器中Fragment对象
        fragmentTransaction.replace(R.id.fl, fragment);
        //提交事务，否则事务不生效
        fragmentTransaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        Mirror mirror = new Mirror();
//        mirror.cal_add(2,3,this);
    }

    @Override
    public void backString(String ss) {
        Toast.makeText(this, "计算结果："+ss, Toast.LENGTH_SHORT).show();
    }
}
